package com.guoqing.sevlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guoqing.entity.Cook;
import com.guoqing.service.CookService;

/**
 * Servlet implementation class Calculate
 */
@WebServlet("/Calculate")
public class Calculate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Calculate() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String date = request.getParameter("date1");
		out.println(date);
		String[] can = request.getParameterValues("can");
		String apartpeople="";
		for(String s : can)
		{
			apartpeople +=","+s;
		}
		out.println(apartpeople.trim().substring(1));
		System.out.println(apartpeople.trim().substring(1));
		String toto = request.getParameter("money");
		double mon = Double.parseDouble(toto);
		out.println(mon);
		String people = request.getParameter("people");
		out.println(people);
		double avg = mon/can.length;
		out.println(avg);
		Cook cook = new Cook();
		SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy");
		Date date1;
		try {
			date1 =  sf.parse(date);
			date1.getTime();
			cook.setDate(new java.sql.Date(date1.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cook.setPeople(apartpeople.trim().substring(1));
		cook.setPaypeople(people);
		cook.setToto(mon);
		cook.setAvg(avg);
		CookService.addCook(cook);
		out.print("添加成功");
		
		
		
	}

}
